package com.epro.dx.widget;

import com.epro.dx.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

/************************
 * @author Tony
 * @Date:2016-3-30下午6:00:13
 * @描述:首页一键过滤切换开关按钮
 ************************/
public class MyMessageSettingsSwitch extends RelativeLayout {

	private Context mContext;
	private ImageButton switchOn;//开启状态
	private ImageButton switchOff;//关闭状态
	
	private boolean isOn = false;//是否处于开启状态
	private MySwitchOnclickListenner mySwitchOnclickListenner;
	public MyMessageSettingsSwitch(Context context) {
		this(context,null);
	}
	public MyMessageSettingsSwitch(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	public MyMessageSettingsSwitch(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		setWillNotDraw(false);
		initView();
		setEvent();
	}
	private void setEvent() {
		switchOn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 关闭开关
				setSwitchState(false);
				if(null!=mySwitchOnclickListenner){
					mySwitchOnclickListenner.onMySwitchClick();
				}
			}
		});
		switchOff.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 开启开关 
				setSwitchState(true);
				if(null!=mySwitchOnclickListenner){
					mySwitchOnclickListenner.onMySwitchClick();
				}
			}
		});
		
		
	}
	private void initView() {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.widget_message_settings_switch, this);
		
		switchOn = (ImageButton) findViewById(R.id.my_filter_switch_on);
		switchOff = (ImageButton) findViewById(R.id.my_filter_switch_off);
	}

	/**
	 * @创建：Tony 2016-3-31上午10:55:06
	 * @描述：设置开关状态
	 * @param isOn true为开启，false为关闭
	 */
	public void setSwitchState(boolean isOn){
		this.isOn = isOn;
		if(isOn){
			switchOn.setVisibility(View.VISIBLE);
			switchOff.setVisibility(View.GONE);
		}else{
			switchOn.setVisibility(View.GONE);
			switchOff.setVisibility(View.VISIBLE);
		}
	}
	
	/**
	 * @创建：Tony 2016-3-31上午10:56:35
	 * @描述：获取当前开关的状态
	 * @return
	 */
	public boolean getSwitchState(){
		return this.isOn;
	}
	
	/**
	 * @创建：Tony 2016-3-31上午11:21:54
	 * @描述：设置控件监听事件
	 * @param myFilterSwitchOnclickListenner
	 */
	public void setSwitchOnclickListenner(MySwitchOnclickListenner mySwitchOnclickListenner){
		this.mySwitchOnclickListenner = mySwitchOnclickListenner;
	}
	/************************
	 * @author Tony
	 * @Date:2016-3-31上午11:19:34
	 * @描述:控件被点击监听
	 ************************/
	public interface MySwitchOnclickListenner{
		void onMySwitchClick();
	}
}
